package com.lab.project.visitor.record.controller;

import com.lab.framework.web.controller.BaseController;
import com.lab.framework.web.domain.AjaxResult;
import com.lab.framework.web.page.TableDataInfo;
import com.lab.project.visitor.domain.DeviceEntity;
import com.lab.project.visitor.domain.vo.UserVO;
import com.lab.project.visitor.domain.vo.VisitorRecordVO;
import com.lab.project.visitor.record.enums.ErrorType;
import com.lab.project.visitor.record.service.IVisitorService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 黄晏凯
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/visitor")
public class VisitorController extends BaseController {
    private IVisitorService visitorService;

    @GetMapping("/device/list")
    public TableDataInfo listDevice() {
        startPage();
        List<DeviceEntity> deviceEntities = visitorService.listDevice();
        return getDataTable(deviceEntities);
    }

    @PostMapping("record/record")
    public AjaxResult saveVisitor(@RequestBody VisitorRecordVO visitorRecordVO) {
        String code = visitorService.saveVisitor(visitorRecordVO);
        if (ErrorType.MESSAGE_SUCCESS.getCode().equals(code)) {
            return AjaxResult.success(ErrorType.getMessageByCode(code));
        } else {
            return AjaxResult.error(ErrorType.getMessageByCode(code));
        }

    }

    @GetMapping("/record/record/list")
    public TableDataInfo listVisitor(@ModelAttribute VisitorRecordVO visitorRecordVO) {
        log.info("========{}==============",visitorRecordVO.getEndTime());
        startPage();
        List<VisitorRecordVO> list = visitorService.listVisitor(visitorRecordVO);
        return getDataTable(list);
    }

    /**
     * 通过身份证号码查询用户
     *
     * @param userIdCard
     * @return
     */
    @GetMapping("/record/user/list")
    public TableDataInfo getVisitorUserByIdCard(String userIdCard) {
        List<UserVO> list = visitorService.getVisitorUserByIdCard(userIdCard);
        for (UserVO vo : list) {
            log.info("================{}", vo);
        }
        return getDataTable(list);
    }

    /**
     * 结束访问
     *
     * @param visitorRecordVO
     * @return
     */
    @PutMapping("/record/record/end")
    public AjaxResult endVisitorStatus(@RequestBody VisitorRecordVO visitorRecordVO) {
        String code = visitorService.endVisitorStatus(visitorRecordVO);
        return AjaxResult.success(ErrorType.getMessageByCode(code));
    }

    @DeleteMapping("/record/record/{id}")
    public AjaxResult deleteVisitorRecord(@PathVariable("id") Long[] ids) {
        System.out.println(ids.getClass().getName());
        String code = visitorService.deleteVisitorRecord(ids);
        return AjaxResult.success(ErrorType.getMessageByCode(code));
    }
}
